<?php
/*
 * 老人出院结算接口
 */
class Apiv1_SettlementController extends Ctrl_Api{
    public function indexAction(){
        $p = $_REQUEST;
        $pDepartment = empty($p['department'])?'':$p['department'];
        $pSid = empty($p['id'])?0:intval($p['id']);
        $pOpt = empty($p['opt'])?'':trim($p['opt']);
        $tOptarr = array('up','down');
        $pSize = empty($p['size'])?5:intval(trim($p['size']));
        
        $tSMO = new SeniorModel;
        $tDIMO = new DepartmentinfoModel;
        $tMIMO = new MedicalinsuranceModel;
        $tPIMO = new PaymentinfoModel;
        $tDsMO = new DiscountinfoModel;
        $tSmMO = new SettlementModel;
        $tLMO = new LeaveModel;

        $tTime = time();

		if(!in_array($pOpt,$tOptarr)){Tool_Fnc::appMsg('操作类型错误');}

        $tWhere = ' 1 and s.leave_status = 2'; 

        if(!empty($pDepartment)){
            $tWhere .= ' and s.department = '.$pDepartment;
        }

        if($pOpt == 'up'){
            $tWhere .= ' and s.id < '.$pSid;
        }else{
            $tWhere .= ' and s.id > '.$pSid;
        }

        $tDay = date('t',strtotime(date('Y-m-d',$tTime)));

        $tSql = 'select count(s.id) c from '.$tSMO->table.' s left join '.$tMIMO->table.' mi on s.id = mi.sid left join '.$tPIMO->table.' pi on pi.num = mi.payment left join '.$tDsMO->table.' ds on mi.charge_discount = ds.num left join '.$tDIMO->table.' di on s.department = di.num where '.$tWhere;
        $tCount = $tSMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];

        if(!empty($tCnt)){
            $tSql = 'select s.id id,s.name name,s.created created,mi.basic_fee basic_fee,pi.introduce payment,pi.num payment_num,ds.introduce discount,ds.num discount_num,di.introduce department from '.$tSMO->table.' s left join '.$tMIMO->table.' mi on s.id = mi.sid left join '.$tPIMO->table.' pi on pi.num = mi.payment left join '.$tDsMO->table.' ds on mi.charge_discount = ds.num left join '.$tDIMO->table.' di on s.department = di.num where '.$tWhere.' order by s.id desc limit 0,'.$pSize;
            $tDatas = $tSMO->query($tSql);
            
            if(!empty($tDatas)){
                foreach($tDatas as $tK=>$tV){
                    //计算总支付
                    $tCount = $tDatas[$tK]['discount'] * 0.1;
                    $tBasicfee = round($tV['basic_fee']/$tDay,2);
                    if($tV['discount_num'] == 1){
                        $tCount = 1;
                    }
                    //判断支付方式
                    if($tV['payment_num'] == 1){#日付
                        $tDatas[$tK]['total_fee'] = $tBasicfee * $tCount;
                    }else if($tV['payment_num'] == 2){#周付
                        $tDatas[$tK]['total_fee'] = $tBasicfee * 7 * $tCount;
                    }else if($tV['payment_num'] == 3){#月付
                        $tDatas[$tK]['total_fee'] = $tV['basic_fee'] * $tCount;
                    }else if($tV['payment_num'] == 4){#季付
                        $tDatas[$tK]['total_fee'] = $tV['basic_fee'] * 3 * $tCount;
                    }else if($tV['payment_num'] == 5){#年付
                        $tDatas[$tK]['total_fee'] = $tV['basic_fee'] * 12 * $tCount;
                    }
                    //是否结算
                    $tSmData = $tSmMO->field('is_pay,fee')->where('sid = '.$tV['id'])->fRow();
                    $tDatas[$tK]['is_pay'] = empty($tSmData['is_pay'])?0:1;
                    $tDatas[$tK]['fee'] = empty($tSmData['fee'])?0:$tSmData['fee'];
                    //获取离院时间
                    $tLData = $tLMO->field('created leavetime')->where('sid = '.$tV['id'])->fRow();
                    $tDatas[$tK]['leavetime'] = empty($tLData['leavetime'])?'':$tLData['leavetime'];
                }
            }
        }

        $tDatas = empty($tDatas)?array():$tDatas;
        Tool_Fnc::ajaxMsg('',1,$tDatas);

    }
    public function editAction(){
        $p = $_REQUEST;

        $tSMO = new SeniorModel;
        $tDIMO = new DepartmentinfoModel;
        $tMIMO = new MedicalinsuranceModel;
        $tPIMO = new PaymentinfoModel;
        $tDsMO = new DiscountinfoModel;
        $tSmMO = new SettlementModel;
        $tLMO = new LeaveModel;

        $tTime = time();
        $tDay = date('t',strtotime(date('Y-m-d',$tTime)));

            $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('老人ID错误'):Tool_Fnc::safe_string($p['id']);
            $pFee = empty($p['fee'])?Tool_Fnc::ajaxMsg('付费金额不能为空'):Tool_Fnc::safe_string($p['fee']);

            $tData = array(
                'sid' => $pId,
                'fee' => $pFee,
                'is_pay' => 1,
                'created' => $tTime,
            );
            if(!$tSmMO->insert($tData)){
                Tool_Fnc::ajaxMsg('操作失败');
            }
            Tool_Fnc::ajaxMsg('操作成功',1);

    }
}
?>
